/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.dolphinscheduler.plugin.task.shell;

import org.apache.commons.lang3.SystemUtils;

import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE;

import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.plugin.task.api.AbstractTask;
import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
import org.apache.dolphinscheduler.plugin.task.api.TaskCallBack;
import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
import org.apache.dolphinscheduler.plugin.task.api.TaskException;
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
import org.apache.dolphinscheduler.plugin.task.api.model.Property;
import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
import org.apache.dolphinscheduler.plugin.task.api.parser.ParamUtils;
import org.apache.dolphinscheduler.plugin.task.api.parser.ParameterUtils;
import org.apache.dolphinscheduler.plugin.task.api.utils.FileUtils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Map;

import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RWXR_XR_X;

/**
 * shell task
 */
public class ShellTask extends AbstractTask {

  /**
   * shell parameters
   */
  private ShellParameters shellParameters;

  /**
   * shell command executor
   */
  private ShellCommandExecutor shellCommandExecutor;

  /**
   * taskExecutionContext
   */
  private TaskExecutionContext taskExecutionContext;

  /**
   * constructor
   *
   * @param taskExecutionContext taskExecutionContext
   */
  public ShellTask(TaskExecutionContext taskExecutionContext) {
    super(taskExecutionContext);

    this.taskExecutionContext = taskExecutionContext;
    this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
        taskExecutionContext,
        logger);
  }

  @Override
  public void init() {
    logger.info("shell task params {}", taskExecutionContext.getTaskParams());

    shellParameters = JSONUtils.parseObject(taskExecutionContext.getTaskParams(), ShellParameters.class);

    if (!shellParameters.checkParameters()) {
      throw new RuntimeException("shell task params is not valid");
    }
  }

  @Override
  public void handle(TaskCallBack taskCallBack) throws TaskException {
    try {
      // construct process
      String command = buildCommand();
      TaskResponse commandExecuteResult = shellCommandExecutor.run(command);
      setExitStatusCode(commandExecuteResult.getExitStatusCode());
      setProcessId(commandExecuteResult.getProcessId());
      shellParameters.dealOutParam(shellCommandExecutor.getVarPool());
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      logger.error("The current Shell task has been interrupted", e);
      setExitStatusCode(EXIT_CODE_FAILURE);
      throw new TaskException("The current Shell task has been interrupted", e);
    } catch (Exception e) {
      logger.error("shell task error", e);
      setExitStatusCode(EXIT_CODE_FAILURE);
      throw new TaskException("Execute shell task error", e);
    }
  }

  @Override
  public void cancel() throws TaskException {
    // cancel process
    try {
      shellCommandExecutor.cancelApplication();
    } catch (Exception e) {
      throw new TaskException("cancel application error", e);
    }
  }

  /**
   * create command
   *
   * @return file name
   * @throws Exception exception
   */
  private String buildCommand() throws Exception {
    // generate scripts
    String fileName = String.format("%s/%s_node.%s",
        taskExecutionContext.getExecutePath(),
        taskExecutionContext.getTaskAppId(), SystemUtils.IS_OS_WINDOWS ? "bat" : "sh");

    File file = new File(fileName);
    Path path = file.toPath();

    if (Files.exists(path)) {
      // this shouldn't happen
      logger.warn("The command file: {} is already exist", path);
      return fileName;
    }

    String script = shellParameters.getRawScript().replaceAll("\\r\\n", "\n");
    script = parseScript(script);
    shellParameters.setRawScript(script);

    logger.info("raw script : {}", shellParameters.getRawScript());
    logger.info("task execute path : {}", taskExecutionContext.getExecutePath());

    FileUtils.createFileWith755(path);
    Files.write(path, shellParameters.getRawScript().getBytes(), StandardOpenOption.APPEND);

    return fileName;
  }

  @Override
  public AbstractParameters getParameters() {
    return shellParameters;
  }

  private String parseScript(String script) {
    // combining local and global parameters
    Map<String, Property> paramsMap = taskExecutionContext.getPrepareParamsMap();
    return ParameterUtils.convertParameterPlaceholders(script, ParamUtils.convert(paramsMap));
  }
}
